package com.saxing.testflux.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.saxing.testflux.R;
import com.saxing.testflux.action.MainAction;
import com.saxing.testflux.action.MainActionCreator;
import com.saxing.testflux.store.MainStore;
import com.saxing.testflux.store.Store;
import com.saxing.testflux.view.base.BaseFluxActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by saxing on 17-7-9.
 */

public class MainActivity extends BaseFluxActivity {

    @BindView(R.id.et_text) EditText editText;
    @BindView(R.id.tv_text) TextView textView;

    private MainStore store;
    MainActionCreator mainActionCreator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDependencies();
    }

    private void initDependencies(){
        mainActionCreator = new MainActionCreator(dispatcher);
        store = new MainStore();
    }

    @OnClick(R.id.btn_send) void onSend(){
        if (editText.getText() != null){
            mainActionCreator.setText(editText.getText().toString());
        }
    }

    @OnClick(R.id.btn_next) void onNest(){
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }

    private void render(MainStore store){
        textView.setText(store.getMessage());
    }

    @Override
    public void onViewUpdate(Object event) {
        if (event instanceof MainStore.MainStoreEvent){
            if (MainAction.ACTION_NEW_MESSAGE.equals(((MainStore.MainStoreEvent)event).getOperationType())){
                render(store);
            }
        }
    }

    @Override
    public Store initStore() {
        return store;
    }
}
